package com.toocms.jinjuhang.dialog.specification

import androidx.databinding.ObservableArrayList
import androidx.databinding.ObservableBoolean
import androidx.databinding.ObservableField
import com.toocms.jinjuhang.BR
import com.toocms.jinjuhang.R
import com.toocms.jinjuhang.data.goods.GetSpecGoodsAttrItemData
import com.toocms.tab.binding.ItemBinding

class SpecificationAttrGroupItemModel(
    val viewModel: SpecificationModel, val item: GetSpecGoodsAttrItemData
) {
    val titleObservableField by lazy {
        ObservableField(item.attr_name)
    }
    val items by lazy {
        val result = ObservableArrayList<SpecificationAttrItemModel>()
        val itemIndex = viewModel.attrItems.indexOf(this@SpecificationAttrGroupItemModel)
        item.attr_values?.forEachIndexed { index, item ->
            //判断是否是默认选择的规格
            val selectedId = viewModel.selectedAttrIds!![itemIndex]
            val isSelected = if (selectedId.isNullOrEmpty()) {
                //记录选择的规格ID
                viewModel.selectedAttrIds!![itemIndex] = item.goods_attr_id
                viewModel.selectedAttrNames!![itemIndex] = item.attr_value
                0 == index
            } else {
                selectedId == item.goods_attr_id
            }
            //添加规格
            result += SpecificationAttrItemModel(
                viewModel,
                this@SpecificationAttrGroupItemModel,
                ObservableField(item),
                ObservableBoolean(isSelected)
            )
        }
        result
    }
    val itemBinding = ItemBinding.of<SpecificationAttrItemModel>(
        BR.specificationAttrItemModel, R.layout.item_specification_attr
    )

}